#!/usr/bin/env bash
# 安装 Docker & docker-compose（采用官方脚本）
set -Eeuo pipefail

type log_info >/dev/null 2>&1 || { echo "[install_docker] _common.sh 未加载"; exit 1; }

install_docker_and_compose() {
  ensure_root
  apt_quiet_update
  # 基础依赖
  DEBIAN_FRONTEND=noninteractive apt-get install -y \
    ca-certificates curl gnupg lsb-release

  # 官方便捷脚本（包含 docker, containerd 等）
  if command -v curl >/dev/null 2>&1; then
    curl -fsSL https://get.docker.com | sh
  else
    log_error "缺少 curl，请先安装网络工具（菜单 12）"
    return 1
  fi

  systemctl enable --now docker

  # 安装 docker-compose（v2 CLI 插件通常随 docker 提供；再兜底装个独立二进制）
  if ! command -v docker-compose >/dev/null 2>&1; then
    DOCKER_COMPOSE_URL="https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)"
    curl -fsSL "$DOCKER_COMPOSE_URL" -o /usr/local/bin/docker-compose || true
    chmod +x /usr/local/bin/docker-compose || true
  fi

  docker --version || true
  docker compose version || docker-compose --version || true
  log_info "Docker 已安装并启动（含 compose 能力）"
}
